//
//  GQJAutoreleaseObjectController.m
//  TotalDemo
//
//  Created by gujian on 16/8/17.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJAutoreleaseObjectController.h"

OBJC_EXTERN int _objc_rootRetainCount(id);
OBJC_EXTERN void _objc_autoreleasePoolPrint();

@interface GQJAutoreleaseObjectController ()

@end

@implementation GQJAutoreleaseObjectController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"autorelease探索——beta0";
    
    //autorelease是否增加引用计数
    NSLog(@"autorelease object retainCount:%d",_objc_rootRetainCount([NSArray array]));

//    NSArray __weak *arr = [NSArray array];
//    NSLog(@"create autorelease object and strong retain it:%d", _objc_rootRetainCount(arr));
//    NSLog(@"1isNil:%d",arr == nil);
    NSArray __weak *arr;
    
    //http://stackoverflow.com/questions/17228530/why-arent-objects-released-when-referencing-them-using-weak-outside-of-an-aut
    _objc_autoreleasePoolPrint();
    @autoreleasepool {
        arr = [NSArray array];
//        NSLog(@"catch the autorelease object :%d", _objc_rootRetainCount(arr));
        NSLog(@"2isNil:%d",arr == nil);
    }
    
    _objc_autoreleasePoolPrint();
//    NSLog(@"after autorelease pool pop:%d",_objc_rootRetainCount(arr));
    NSLog(@"3isNil:%d",arr == nil);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
